package com.example.dllo.exe.fenlei.gonglue;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.dllo.exe.R;
import com.example.dllo.exe.bean.GongLueBean;
import com.example.dllo.exe.bean.GongLueRvBean;
import com.example.dllo.exe.main.BaseFragment;
import com.example.dllo.exe.fengzhuang.NetTool;
import com.example.dllo.exe.fengzhuang.DBValue;
import com.google.gson.Gson;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class GongLueFragemnt extends BaseFragment implements View.OnClickListener {
   private TextView gongluechakanquanbu,gongluepinleichakanquanbu,gonglueduixiangchakanquanbu;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private GongLueRvBean arraysbean;
    private RecyclerView recyclerViewgongluedown, recyclerViewgongluedown1, recyclerViewgongluedown2, recyclerViewgonglueup;


    @Override
    protected int setLayout() {
        return R.layout.gongluefragmentlayout;
    }

    @Override
    protected void initView() {
        recyclerViewgonglueup = (RecyclerView) getView().findViewById(R.id.gonglue_recycleviewup);
        recyclerViewgongluedown = (RecyclerView) getView().findViewById(R.id.gongluerecycleviewdown);
        recyclerViewgongluedown1 = (RecyclerView) getView().findViewById(R.id.gongluerecycleviewdown1);
        recyclerViewgongluedown2 = (RecyclerView) getView().findViewById(R.id.gongluerecycleviewdown2);
        gongluechakanquanbu= (TextView) getView().findViewById(R.id.gonglue_lanmuchakanquanbu);
        gongluepinleichakanquanbu=bindView(R.id.gongluepinleichakanquanbu_text);
        gonglueduixiangchakanquanbu=bindView(R.id.gonglueduixiangchakanquanbu_text);

    }

    @Override
    protected void initData() {
        NetTool netTool = new NetTool();
        netTool.getNetData(DBValue.FENLEI_GONGLUEDOWN, GongLueBean.class, new NetTool.NetListener<GongLueBean>() {
            @Override
            public void onSuccess(GongLueBean gongLueBean) {
                try {
                    GongLueRvDownAdapter gongLueRvDownAdapter = new GongLueRvDownAdapter(getContext());
                    GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
                    recyclerViewgongluedown.setLayoutManager(manager);
                    gongLueRvDownAdapter.setGongLueBean(gongLueBean);
                    gongLueRvDownAdapter.setI(0);
                    recyclerViewgongluedown.setAdapter(gongLueRvDownAdapter);

                    GridLayoutManager manager1 = new GridLayoutManager(getContext(), 2);
                    GongLueRvDownAdapter gongLueRvDownAdapter1 = new GongLueRvDownAdapter(getContext());
                    recyclerViewgongluedown1.setLayoutManager(manager1);
                    gongLueRvDownAdapter1.setI(1);
                    gongLueRvDownAdapter1.setGongLueBean(gongLueBean);
                    recyclerViewgongluedown1.setAdapter(gongLueRvDownAdapter1);

                    GridLayoutManager manager2 = new GridLayoutManager(getContext(), 2);
                    GongLueRvDownAdapter gongLueRvDownAdapter2 = new GongLueRvDownAdapter(getContext());
                    recyclerViewgongluedown2.setLayoutManager(manager2);
                    gongLueRvDownAdapter2.setI(2);
                    gongLueRvDownAdapter2.setGongLueBean(gongLueBean);
                    recyclerViewgongluedown2.setAdapter(gongLueRvDownAdapter2);


                    arraysbean = new GongLueRvBean();
                    requestQueue = Volley.newRequestQueue(getContext());
                    //解析
                    stringRequest = new StringRequest(DBValue.FENLEI_GONGLUERVUP, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            Gson gson = new Gson();
                            GongLueRvUpAdapter gongLueRvUpAdapter = new GongLueRvUpAdapter(getContext());
//                        LinearLayoutManager manager4 = new LinearLayoutManager(getContext());
                            GridLayoutManager manager4 = new GridLayoutManager(getContext(), 3);

                            manager4.setOrientation(LinearLayoutManager.HORIZONTAL);
                            recyclerViewgonglueup.setLayoutManager(manager4);

                            arraysbean = gson.fromJson(response, GongLueRvBean.class);
                            gongLueRvUpAdapter.setGongLueRvBean(arraysbean);
                            recyclerViewgonglueup.setAdapter(gongLueRvUpAdapter);
                            gongLueRvUpAdapter.setItemonClick(new GongLueRvUpAdapter.Itemclick() {
                                @Override
                                public void Itemclick(int pos, String name) {
                                    Intent intent=new Intent(getContext(),GongLuePinJieAty.class);
                                    int url = arraysbean.getData().getColumns().get(pos).getId();
                                    String urlpinjie = DBValue.FENLEI_GONGLUEQIANZHUIUP + url + DBValue.FENLEI_GONGLUEHOUZHUIDOWN;

                                    intent.putExtra("key",urlpinjie);
                                    intent.putExtra("name", name);
                                    intent.putExtra("url",url);
                                    startActivity(intent);

                                }
                            });

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    });
                    requestQueue.add(stringRequest);
                }catch (Exception e){

                }


            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        gongluechakanquanbu.setOnClickListener(this);
        gongluepinleichakanquanbu.setOnClickListener(this);
        gonglueduixiangchakanquanbu.setOnClickListener(this);

    }


    @Override
    public void onClick(View view) {
       switch (view.getId()){
           case R.id.gonglue_lanmuchakanquanbu:
               Intent intent=new Intent(getContext(),GongLueLanMuChaKanQuanBu.class);
               startActivity(intent);
               break;
           case R.id.gongluepinleichakanquanbu_text:
               Intent intent1=new Intent(getContext(),GongLuePinLeiChaKanQuanBu.class);
               startActivity(intent1);

               break;
           case  R.id.gonglueduixiangchakanquanbu_text:
               Intent intent2=new Intent(getContext(),GongLueDuiXiangChaKanQuanBu.class);
               startActivity(intent2);
               break;
       }
    }

}
